package com.smap.core;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.smap.common.Utils;

/**
 * an object on map
 *
 * @author Shen
 */
public class Marker extends Composite {
	public XY location;
	private HTML html;
	private Map map;
	AbsolutePanel root;
	AbsolutePanel rootFront;
	boolean interactive = false;

	public Marker(double x, double y, String innerHtml, boolean interactive) {
		super();
		this.location = new XY(x, y);
		this.interactive = interactive;
		this.initWidget(html = new HTML());
		html.setHTML(innerHtml);
	}

	public void install(Map map) {
		this.map = map;
	}

	public void draw() {
		if (root == null) {
			root = this.map.getRoot();
			rootFront = this.map.getRootFront();
			this.addToMap();
		}
		setPosition(this.map.mapToPixel(this.location));
	}

	public Marker project(Projection proj) {
		proj.project(location);
		return this;
	}

	public void addToMap() {
		if (interactive) {
			rootFront.add(this);
		} else {
			Utils.setZIndex(this, 100);
			root.add(this);
		}
	}

	public void setPosition(XY xy) {
		Utils.setPosition(this, (int) xy.x, (int) xy.y);
	}

}
